import pymysql
def main():
    conn = pymysql.connect(host='10.1.20.6',port=3306,
                           user='root',password='admin@9000',
                           db='my_database',charset='utf8')
    try:
        with conn.cursor() as cursor:
            result = cursor.execute("insert into tb_dept values(40,'运维部','上海')")
            if result == 1:
                print("添加成功")
            conn.commit()
    except pymysql.MySQLError as error:
        print(error)
        conn.rollback()
    finally:
        conn.close()
    print(conn)
if __name__ == '__main__':
    main()